<?php
	/**
  * @todo 校验xls文件
  * @copyright gaoxiang 2009
  * @author gaoxiang
  * @createdate 2009.05.12  汶川大地震一周年
  * @license 本程序遵照Creative Commons 署名-非商业性使用-相同方式共享 2.5
  * 中国大陆协议，该协议的中文版本http://creativecommons.org/licenses/by-nc-sa/2.5/cn/
  **/ 
	require_once(dirname(dirname(__FILE__)) . '/require/load.php');
	check_user();
	if($_GET['action']=='upload'){?>
		<script type="text/javascript">
			$(function() { $("input.fileup").filestyle({image: "../images/addfile.gif",imageheight : 20,imagewidth : 90,width : 100 });});
		</script>
	<style type="text/css">
		.fileup{background:#fff;color:#888;}.filesub{background: #fff url("../images/rx.gif") top left repeat-x; display:inline;width:68px;height:20px;margin-left:100px;padding:0;}
	</style>
		<div class="log">
			<fieldset>
				<legend align="center"><?php if($_GET['type']='rog') echo '原材料表格记录测试'; elseif($_GET['type']='rog') echo '生产表格记录测试';?></legend>
				<form enctype="multipart/form-data" action="checkxls.php?action=check&type=<?php if($_GET['type']='rog') echo 'rog'; elseif($_GET['type']='produce') echo 'produce';?>" method="POST">
							<input type="file" name="fileup" id="fileup" class="fileup" align="absmiddle"/>
							<input type="submit" value="上传文件" class="filesub"/>
				</form>
			</fieldset>
		</div>
	<?php }
	elseif($_GET['action']=='check'){
		require_once('PHPExcel.php');
		require_once('PHPExcel/Reader/Excel5.php');
		require_once ('PHPExcel/IOFactory.php');
		if($_GET['type']=='rog'){
			set_refer();
			if (isset($_FILES['fileup']) && $_FILES['fileup']["error"]==UPLOAD_ERR_OK){
				$f_name=$_FILES['fileup']['name'];//获取上传源文件名
        $uploadfile=$uploaddir.strtolower(basename($f_name));              
        $tmp_type=substr(strrchr($f_name,"."),1);//获取文件扩展名
				$tmp_type=strtolower($tmp_type); 
        if($tmp_type!='xls'){ 
					login_echo($_SESSION['referer'],"对不起,不能上传".$tmp_type."格式文件, ".$f_name." 文件上传失败!"); 
				}elseif ($_FILES['fileup']['size']>10*1024*1024){			
          login_echo($_SESSION['referer'],"对不起,你上传的文件 ".$f_name." 容量为".round($_FILES['fileup']['size']/1024/1024,1)."MB,大于限制的10MB,上传失败!"); 
        }else{
					$t=date("ymdHis").substr($gettime[0],2,6).$rand;
					$attdir="file/";//将文件移至的目录，请根据自己情况修改，相对于该脚本路径  
    			if(!is_dir($attdir))  
    				mkdir($attdir);
      			$uploadfile=$attdir.$t.".".$tmp_type; 
            if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadfile)){ 
							$objReader = new PHPExcel_Reader_Excel5();//新建一个excel5对象
							$objReader->setReadDataOnly(true);//设置为只读
							$objPHPExcel = $objReader->load($uploadfile);//加载xls文件
		        	$objExcelActive=$objPHPExcel->getActiveSheet();//获得第一个活动的主页
		        	$highestRow = $objExcelActive->getHighestRow(); // 获得最高行数 e.g. 10
		        	$highestColumn = $objExcelActive->getHighestColumn(); //获得最高列数字母 e.g 'F'
		        	$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//获得最高列数索引 e.g '10'
		        	/*转换编码为utf-8*/
		        	$internalEncoding = "UTF-8";
		        	$outputEncoding = "UTF-8";
		        	$inputEncoding = "UTF-8";
		        	iconv_set_encoding("internal_encoding", $internalEncoding);
		        	iconv_set_encoding("output_encoding", $outputEncoding);
		        	iconv_set_encoding("input_encoding", $inputEncoding);
		        	ob_start("ob_iconv_handler");//快速转换编码
							if($highestColumn=="N"){
								$err_num=0;
								$err_msg='';
								for($i=2;$i<=$highestRow;++$i){
		        			$supplier=$objExcelActive->getCellByColumnAndRow(0, $i)->getValue();//生产厂家									
									if(!checksingle($supplier,"alpha")){
										++$err_num;
										$err_msg.='<p>第'. $i .'行，厂家代码错误。</p>';								
									}
									$make=$objExcelActive->getCellByColumnAndRow(1, $i)->getCalculatedValue();//牌号
									if(!in_array($make,$arr_make)){
										++$err_num;
										$err_msg.='<p>第'. $i .'行，牌号错误。</p>';
									}
									$spec=$objExcelActive->getCellByColumnAndRow(2, $i)->getCalculatedValue();//规格
									if($spec != 9.5 && $spec != 12 && $spec != 12.5){
										++$err_num;
										$err_msg.='<p>第'. $i .'行，牌号不为9.5、12或者12.5。</p>';
									}
									$weight=$objExcelActive->getCellByColumnAndRow(3, $i)->getCalculatedValue();//weight
									if(!is_number($weight,0)){
										++$err_num;
										$err_msg.='<p>第'. $i .'行，重量不为数字。</p>';	
									}
									$origin=$objExcelActive->getCellByColumnAndRow(4, $i)->getValue();//厂家批号
									if(!$origin){
										++$err_num;
										$err_msg.='<p>第'. $i .'行，厂家批号不能为空。</p>';
									}
									$raw=$objExcelActive->getCellByColumnAndRow(5, $i)->getCalculatedValue();//原料批号
									if(!is_rog($raw) || substr($raw,1,1) != $supplier){
										++$err_num;
										$err_msg.='<p>第'. $i .'行，原材料批号不符合规则。</p>';
									}
									$tensile=$objExcelActive->getCellByColumnAndRow(6, $i)->getCalculatedValue();//原料Rm
									if($tensile != "" && !is_number($tensile,0) && (int)$tensile <= 0){
										++$err_num;
										$err_msg.='<p>第'. $i .'行，原材料抗拉强度错误。</p>';
									}
									$extend=$objExcelActive->getCellByColumnAndRow(7, $i)->getCalculatedValue();//原料A
									if($extend != "" && !is_number($extend,0)){
										++$err_num;
										$err_msg.='<p>第'. $i .'行，原材料伸长率错误。</p>';
									}
									$anneal=$objExcelActive->getCellByColumnAndRow(8, $i)->getCalculatedValue();//退火批号
									if($anneal){
										if(!is_rog($anneal)){
											++$err_num;
											$err_msg.='<p>第'. $i .'行，退火批号不符合规则。</p>';
										}
										$technics=$objExcelActive->getCellByColumnAndRow(9, $i)->getCalculatedValue();//退火工艺
										$atensile=$objExcelActive->getCellByColumnAndRow(10, $i)->getCalculatedValue();//退火Rm
										if($atensile != "" && !is_number($atensile,0)){
											++$err_num;
											$err_msg.='<p>第'. $i .'行，退火后抗拉强度错误。</p>';
										}
										$aextend=$objExcelActive->getCellByColumnAndRow(11, $i)->getCalculatedValue();//退火A
										if($aextend != "" && !is_number($aextend,0)){
											++$err_num;
											$err_msg.='<p>第'. $i .'行，退火后伸长率错误。</p>';
										}
									}
									$judge=$objExcelActive->getCellByColumnAndRow(12, $i)->getCalculatedValue();//judge
									if($judge != "合格" && $judge != "不合格"){
										++$err_num;
										$err_msg.='<p>第'. $i .'行，判定结果错误。</p>';
									}								
								}
								if($err_num)
		        				echo '<div style="margin-left:350px;"><p>共有' . $err_num . '处错误。</p>' . $err_msg .'</div>';
		        		unlink ($uploadfile);
		        	}else{
		        		login_echo($_SESSION['referer'],"文件列数不正确，请检查！");
		        		unlink ($uploadfile);
	        		}
						}else{
							login_echo($_SESSION['referer'],$f_name.' 上传失败');
						}
				}
			}else{
				login_echo($_SESSION['referer'],"对不起,文件大小超过限制！");
			}
		}
	}
	html_footer();
?>